//
//  KTCPrivacyViewController.m
//  Keping
//
//  Created by 柯平 on 2017/10/9.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "KTCPrivacyViewController.h"
#import "KTCFAQRequest.h"
#import "KTCPFAQModel.h"
#import <UITableView+FDTemplateLayoutCell.h>
#import "KPrivacyCell.h"
#import "ZMZWebContentView.h"

@interface KTCPrivacyViewController ()

@property(nonatomic,weak)ZMZWebContentView *webBaseView;
@property (nonatomic, strong) UIProgressView *progressView;

@end

@implementation KTCPrivacyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"T&C/Privacy";

    [self getData];
}

-(void)getData
{
    [super getData];
    NSString *urlstr;
    if ([self.TCPStr isEqualToString:@"Privacy Policy"]) {
        urlstr = @"privacy";
    } else {
        urlstr = @"FAQ";
    }
    WEAKSELF
    KTCFAQRequest* request = [[KTCFAQRequest alloc] initWithType:urlstr];
    [request startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
        KTCPFAQModel* data = [KTCPFAQModel modelWithJSON:request.responseData];
        if (data.statusCode == 200) {
            
            
            ZMZWebContentView *webView = [[ZMZWebContentView alloc] initWithFrame:self.view.bounds];
            webView.backgroundColor = kWhiteColor;
            WEAKSELF
            [webView setShouldStartLoadBlock:^BOOL(WKWebView *webView, NSURLRequest *request) {
                [weakSelf showLoading];
                return YES;
            }];
            [webView setFinshLoadBlock:^(WKWebView *webView) {
                [weakSelf hideHUD];
            }];
            webView.propressBlock = ^(CGFloat propress) {
                weakSelf.progressView.progress = propress;
                if (propress == 1.0) {
                    [UIView animateWithDuration:0.25f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
                        weakSelf.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.4f);
                    } completion:^(BOOL finished) {
                        weakSelf.progressView.hidden = YES;
                    }];
                } else {
                    weakSelf.progressView.hidden = NO;
                }
            };
            webView.titleBlock = ^(NSString *title) {
                weakSelf.title = title;
            };
            [weakSelf.view addSubview:webView];
            weakSelf.webBaseView = webView;
            for (KPrivacyModel *model in data.privacyEntityViews) {
                if ([weakSelf.TCPStr isEqualToString:model.title]) {
                    [weakSelf.webBaseView loadRequestWithURLString:[model.url stringByReplacingOccurrencesOfString:@" " withString:@""]];
                }
            }
            
            weakSelf.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 2)];
            weakSelf.progressView.backgroundColor = [UIColor blueColor];
            //设置进度条的高度，下面这句代码表示进度条的宽度变为原来的1倍，高度变为原来的1.5倍.
            weakSelf.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
            [weakSelf.view addSubview:weakSelf.progressView];
        }else{
            [weakSelf showErrorText:data.msg errCode:data.statusCode];
        }
    } failure:^(__kindof KBaseRequest * _Nonnull request) {
        [weakSelf showErrorText:request.responseString];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
